var express = require('express');
var router = express.Router();
var User = require('../models/user');
var Post = require('../models/post');

/* GET users listing. */
router.get('/:user', function (req, res) {
  User.get(req.params.user, function (err, user) {
    if (!user) {
      req.flash('error', '用户不存在');
      return res.redirect('/');
    }

    Post.get(user.name, function (err, posts) {
      if (err) {
        req.flash('error', err);
        return res.redirect('/');
      }

      res.render('user', {
        title: user.name,
        posts: posts
      });
    });
  });
});


/* GET user personal. */
router.get('/user/personal', function (req, res) {
  var currentUser = req.session.user;
  User.get(currentUser.name, function (err, user) {
    if (!user) {
      req.flash('error', '用户不存在');
      return res.redirect('/');
    }

      res.render('personal', {
        title: '个人中心',
        username: user.name,
        nickname: user.nickname,
        password: user.password
      });
    });
  });

module.exports = router;
